import http from '../../../utils/http.js';


//基本信息
const baseField = [{
  "text": "业务类型",
  "field": "tradeVehicleType",
  select: true,
}, {
  "text": "过户类型",
  "field": "transferType",
  select: true,
}, {
  "text": "转入地",
  "field": "destination",
  region: true,
}, {
  "text": "车辆价格",
  "field": "amount",

}];

const vehicleField = [{
  "text": "登记证书编号",
  "field": "vehicle.registerNo",

}, {
  "text": "车架号",
  "field": "vehicle.vin",

}, {
  "text": "车牌号码",
  "field": "vehicle.plateNo",

}, {
  "text": "品牌型号",
  "field": "vehicle.brandModel",

  // }, {
  //   "text": "车辆品牌",
  //   "field": "vehicle.brand",
  //   rule: {
  //     //"required": true,
  //     "maxlength": 64,
  //   },
  //   message: {
  //     //"required": "车辆品牌不能为空",
  //     "maxlength": "车辆品牌不能超过64个字符",
  //   }
  // }, {
  //   "text": "车辆型号",
  //   "field": "vehicle.model",
  //   rule: {
  //     //"required": true,
  //     "maxlength": 64,
  //   },
  //   message: {
  //     //"required": "车辆型号不能为空",
  //     "maxlength": "车辆型号不能超过64个字符",
  //   }
}, {

  "text": "出厂日期",
  "field": "vehicle.productionDate",
  date: true,
}, {
  "text": "发动机型号",
  "field": "vehicle.engineModel",

}, {
  "text": "发动机号",
  "field": "vehicle.engineNo",

}, {
  "text": "车辆类型",
  "field": "vehicle.vehicleType",
  select: true
}, {
  "text": "燃油种类",
  "field": "vehicle.fuelType",
  select: true
}, {
  "text": "使用性质",
  "field": "vehicle.useProperty",
  select: true
}, {
  "text": "是否进口车",
  "field": "vehicle.isImported",
  select: true
}, {
  "text": "排放标准",
  "field": "vehicle.emissionStandard",
  select: true
}, {
  "text": "号牌种类",
  "field": "vehicle.plateType",
  select: true
}];

const buyer = [{
  //   "text": "证件类型",
  //   "field": "buyer.creditType",
  //   select: true
  // }, {
  "text": "证件编号",
  "field": "buyer.creditNo",

}, {
  "text": "买方名称",
  "field": "buyer.name",

}, {
  "text": "联系电话",
  "field": "buyer.tel",

  //暂时不要户籍地址
  // }, {
  //   "text": "户籍地址",
  //   "field": "buyer.censusAddress",
  //   rule: {
  //     "maxlength": 128
  //   },
  //   message: {
  //     "maxlength": "户籍地址不能超过128个字符"
  //   }
}, {
  "text": "居住地址",
  "field": "buyer.residenceAddress",
  "textarea": true

},
  {
    "text": "联系电话",
    "field": "buyer.tel",

    //暂时不要户籍地址
    // }, {
    //   "text": "户籍地址",
    //   "field": "buyer.censusAddress",
    //   rule: {
    //     "maxlength": 128
    //   },
    //   message: {
    //     "maxlength": "户籍地址不能超过128个字符"
    //   }
  }, ];

const seller = [{
  //   "text": "证件类型",
  //   "field": "seller.creditType",
  //   select: true
  // }, {
  "text": "证件编号",
  "field": "seller.creditNo",

}, {
  "text": "卖方名称",
  "field": "seller.name",

}, {
  "text": "联系电话",
  "field": "seller.tel",

  //暂时不要户籍地址
  // }, {
  //   "text": "户籍地址",
  //   "field": "seller.censusAddress",
  //   rule: {
  //     "maxlength": 128
  //   },
  //   message: {
  //     "maxlength": "户籍地址不能超过128个字符"
  //   }
}, {
  "text": "居住地址",
  "field": "seller.residenceAddress",
  "textarea": true

},
  {
    "text": "联系电话",
    "field": "seller.tel",

    //暂时不要户籍地址
    // }, {
    //   "text": "户籍地址",
    //   "field": "seller.censusAddress",
    //   rule: {
    //     "maxlength": 128
    //   },
    //   message: {
    //     "maxlength": "户籍地址不能超过128个字符"
    //   }
  }, ];


Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeheight:0,
    currentStep: 0,
    formField: [
      baseField, vehicleField, buyer, seller
    ],
    files: {},
    filesFlag: {},
    filesField: [{
      text: '行驶证',
      field: 'vehicleLicense'
    }, {
      text: '机动车登记证',
      field: 'vehicleRegister'
    }, {
      text: '买家身份证（正）',
      field: 'buyerCreditFront'
    }, {
      text: '买家身份证（反）',
      field: 'buyerCreditBack'
    }, {
      text: '卖家身份证（正）',
      field: 'sellerCreditFront'
    }, {
      text: '卖家身份证（反）',
      field: 'sellerCreditBack'
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
 

    if (options.applicationId != null) {
      //加载要修改的数据
      http.request({
        url: '/wxApplication/findApplicationDetail',
        data: {
          applicationId: options.applicationId
        },
        success: (res) => {
          this.transferData(res, "buyer");
          this.transferData(res, "seller");
          this.transferData(res, "vehicle");
          this.transferData(res, "ticket");

          res["vehicle.isImported"] = res["vehicle.isImported"] == 1 ? '是' : '否';
          let filesFlag = {};
          let image = {};
          if (res["image"] != null) {
            let array = this.data.filesField;
            for (let i = 0; i < array.length; i++) {
              let field = array[i].field;
              if (res["image"][field] != null && res["image"][field] != '') {
                let src = http.apiUrl + http.imgUrl + "?imagePath=" + res["image"][field] +
                  "&" + wx.getStorageSync("sessionid")
                image[field] = encodeURI(src);
              }
            }
          }

          this.setData({
            formData: res,
            files: image
          });
        }
      });

    }
  },
  transferData: function(res, field) {
    let buyer = res[field];
    for (let key in buyer) {
      res[field + "." + key] = buyer[key];
    }
    delete res[field];
  },
  //调用验证函数
  next: function(e) {
    this.setData({
      currentStep: this.data.currentStep + 1
    });
  },
  //上一步
  previous: function(e) {
    this.setData({
      currentStep: this.data.currentStep - 1
    });
  },
  previewImage: function(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.img, // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.img] // 需要预览的图片http链接列表
    })
  },
  goBack: function(e) {
    wx.navigateBack({
      delta: 1
    })
  }
})